package dasregistry.ontology.browsing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.biojava.services.das.dao.TypesDAO;
import org.json.simple.JSONObject;
import org.json.simple.parser.ContainerFactory;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class OLSOntologyReader {
	TypesDAO typesDao;
	Map<String, Set<Long>> types;

	public boolean getOntologyAsJSON(String urlString, Writer out) {
		// String urlString=request.getParameter("url");
		
			//types = typesDao.getTypesOntologiesWithAutoId();
		
		// response.setHeader("Content-Type", "text/xml");
		// response.setContentType("text/xml");
		urlString = urlString.replace(" ", "+");
		System.out.println(urlString);
		// %3A :
		// something wierd going on with the front ends so I have to do a
		// replace so search works
		// urlString=urlString.replace("%3A",":");
		// System.out.println("urlString="+urlString);
		URLConnection connection = null;
		try {
			connection = new URL(urlString).openConnection();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// String contentEncoding =
		// connection.getHeaderField("Content-Encoding");//should
		// be"Content-Encoding","ISO-8859-1")
		// System.out.println("content type="+contentEncoding);
		// if (contentType.startsWith("text")) {
		// String charset = "ISO-8859-1";//this encoding is what the registry
		// uses so must be set here to override default;
		// System.out.println("charset="+charset);

		JSONParser parser = new JSONParser();
		ContainerFactory containerFactory = new ContainerFactory() {
			public List creatArrayContainer() {
				return new LinkedList();
			}

			public JsonNode createObjectContainer() {
				return new JsonNode();
			}

		};

		BufferedReader reader = null;
		//FileWriter out = null;
//		try {
//			out = new FileWriter(
//					"/Users/jw12/workspaceJune2009/dasregistry/WebContent/tree/_docs/_json_data.json");
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}

		try {

			reader = new BufferedReader(new InputStreamReader(connection
					.getInputStream(), "ISO-8859-1"));
			JsonNode json = null;// initial object created from json from ols
			JsonNode newJson = null;// new object created for jstree
			for (String line; (line = reader.readLine()) != null;) {
System.out.println("line="+line);
				try {
					json = (JsonNode) parser.parse(line, containerFactory);
					System.out.println(json);
					// creat a new json object that suits the jstree plugin
					// format for drawing trees
					newJson = json.createNew(json);
					System.out.println("new Json=" + newJson);
				} catch (ParseException pe) {
					System.out.println(pe);
				}

				// String newLine=line.replace("id","data" );
				// writer.write(line);
				System.out.println("line=" + line);
			}
			JSONObject.writeJSONString(newJson, out);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public TypesDAO getTypesDao() {
		return typesDao;
	}

	public void setTypesDao(TypesDAO typesDao) {
		this.typesDao = typesDao;
	}

}
